package com.vslimit.servlet;

import com.vslimit.command.VoaUrlCommand;
import com.vslimit.dto.VoaCatDTO;
import com.vslimit.dto.VoaUrlDTO;
import com.vslimit.statement.VoaUrlStatement;
import com.vslimit.util.FuniApplicationContext;
import com.vslimit.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: vslimit
 * Date: 12-4-6
 * Time: 下午1:56
 * To change this template use File | Settings | File Templates.
 */
public class VoaUrlListServlet extends HttpServlet {

    private VoaUrlCommand voaUrlCommand;

    @Override
    public void init() throws ServletException {
        voaUrlCommand = (VoaUrlCommand) FuniApplicationContext.getBean("voaUrlCommand");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer pageNumber = StringUtil.isEmpty(request.getParameter("pageNumber")) ? 1 : Integer.valueOf(request.getParameter("pageNumber"));
        Integer catId = Integer.valueOf(request.getParameter("catId"));
        voaUrlCommand.setListStatement(VoaUrlStatement.FIND_URL_BY_CAT);
        voaUrlCommand.setPageNumber(pageNumber);
        voaUrlCommand.setCatId(catId);

        List<VoaUrlDTO> list = voaUrlCommand.getPageList();

        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<body>");

        for (VoaUrlDTO dto : list) {
            sb.append(dto.getId());
            sb.append("\t");
            sb.append("<a href=voaCatServlet?id=" + dto.getId() + ">");
            sb.append(dto.getName());
            sb.append("</a>");
            sb.append("<br/>");
        }
        sb.append("</body>");
        sb.append("</html>");

        PrintWriter out = response.getWriter();
        out.print(sb.toString());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
